const T = getApp().globalData.T
const config = getApp().globalData.config

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    token: '',
    postList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { id, name } = options;
    this.data.id = id;
    this.data.token = getApp().globalData.token;
    wx.setNavigationBarTitle({
      title: name,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPostList();
  },

  /**
   * 获取帖子列表
   */
  getPostList() {
    T.req({
      loadTitle: '请求中', 
      url: config.api + 'forum/topic/list',
      method: 'GET',
      data: {
        token: this.data.token,
        boardId: this.data.id
      },
      success: (res) => {
        this.data.isPermission = res.isPermission;
        this.setData({ postList: res.list })
      }
    })
  },

  onCheckPostDetail(e) {
    const id = e.detail.value;
    wx.navigateTo({
      url: '../detail/detail?id=' + id
    });
  },

  onAddPost() {
    if (!this.data.isPermission) {
      T.toast({ id: '#toast', text: '你暂没有权限发布' });
      return;
    }
    wx.navigateTo({
      url: '../add/add'
    })
  }
})